using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace WillStott.MxAppToolbox
{
    public class Utils
    {
        public static string StrLFCR = "\r\n";

        public static string GetPath(string pathFilename)
        {
            return pathFilename.Substring(0, pathFilename.LastIndexOf('\\') + 1);
        }

        public static string GetFilename(string pathFilename)
        {
            string rc = pathFilename;
            int start = pathFilename.LastIndexOf('\\');
            if ((start > -1) && ( (start+1) < pathFilename.Length))
                rc = pathFilename.Substring(start+1);
            return rc;
        }

        public static string GetFileType(string pathFilename)
        {
            string rc = "";
            int start = pathFilename.LastIndexOf('.');
            if ((start > -1) && ( (start+1) < pathFilename.Length))
                rc = pathFilename.Substring(start+1);
            return rc;
        }

        public static string GetAppSettingString(AppSettingsSection appSettings, string keyName)
        {
            string rc = null;
            if (appSettings != null)
            {
                KeyValueConfigurationCollection settings = appSettings.Settings;
                string[] names = appSettings.Settings.AllKeys;
                foreach (string name in names)
                {
                    if (name == keyName)
                    {
                        rc = settings[name].Value;
                    }
                }
            }
            return rc;
        }

        public static int GetAppSettingInt(AppSettingsSection appSettings, string keyName)
        {
            int rc = -1;
            try
            {
                string value = Utils.GetAppSettingString(appSettings, keyName);
                if (value != null)
                    rc = int.Parse(value);
            }
            catch (Exception)
            {
            }
            return rc;
        }
    }
}
